package com.dean.cmd;

import com.dean.dao.DeanToolDao;
import com.dean.entity.DeanExportInfo;
import com.dean.entity.DeanImportInfo;
import com.dean.entity.SysDbConnInfo;
import com.dean.service.DataFileImpExpService;
import com.dean.service.impl.CsvImpExpServiceImpl;
import com.dean.service.impl.ExcelImpExpServiceImpl;
import com.dean.utils.DeanApacheCsvUtils;
import com.dean.utils.DeanXmlParseUitls;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;

import java.util.List;
import java.util.Scanner;

public class DeanToolMain {

    public static void main(String[] args) {
        System.out.println("hello welcome to use 'dean tool'");
        mainMenu();
    }

    public static void mainMenu() {
        System.out.println("功能选择:");
        // 读取功能列表
        System.out.println("1.excel导入");
        System.out.println("2.excel导出");
        System.out.println("3.csv导入");
        System.out.println("4.csv导出");
        System.out.println("5.xml2csv");
        System.out.println("9.退出");

        Scanner input = new Scanner(System.in);
        String checkMenu = input.nextLine();
        if(!StringUtils.isNumeric(checkMenu)){
            System.out.println("非法操作,将返回上一步操作");
            mainMenu();
            return;
        }
        int check = Integer.parseInt(checkMenu);
        menuImp(check);
    }

    public static void menuImp(int checkMenu){
        switch (checkMenu){
            case 1:
                excelImp();
                break;
            case 2:
                excelExp();
                break;
            case 3:
                csvImp();
                break;
            case 4:
                csvExp();
                break;
            case 5:
                xml2csv();
                break;
            case 9:
                System.exit(0);
                break;

        }
        // 重新加载菜单
        mainMenu();
    }

    public static void xml2csv(){
        System.out.println("请输入要转换的xml文件名");
        String fileName = new Scanner(System.in).nextLine();
        System.out.println("请输入遍历的节点");
        String loopNode = new Scanner(System.in).nextLine();
        try {
            List<List<String>> datas = DeanXmlParseUitls.docParse2List(fileName,loopNode);
            DeanApacheCsvUtils.writeCsv(fileName.replace(".xml",".csv"),",",datas);

        } catch (DocumentException e) {
            System.out.println("执行失败,错误信息:"+e.getMessage());
            xml2csv();
        }
        System.out.println("执行成功,转换完成");

    }

    public static void csvImp(){
        System.out.println("请选中需要导入的数据库");
        List<SysDbConnInfo> connInfos = DeanToolDao.getInstance().queryConnInfo();
        for (int i = 0; i < connInfos.size(); i++) {
            System.out.println(i+"."+connInfos.get(i).getConnName());
        }
        // conn 连接选择
        String userInput = new Scanner(System.in).nextLine();
        if(!StringUtils.isNumeric(userInput)){
            System.out.println("非法操作,将返回上一步操作");
            csvImp();
            return;
        }
        DataFileImpExpService service = new ExcelImpExpServiceImpl();
        DeanImportInfo importInfo = new DeanImportInfo();
        // 导入文件名
        int check2 = Integer.parseInt(userInput);
        // 导入表名
        System.out.println("请输入要导入的表名");
        String tableName = new Scanner(System.in).nextLine();
        // 导入字段
        System.out.println("请输入要导入的字段名,字段名用逗号分割");
        String fieldStr = new Scanner(System.in).nextLine();
        // 导入文件名
        System.out.println("请输入要导入的文件名");
        String fileName = new Scanner(System.in).nextLine();
        // 分隔符
        System.out.println("请输入分割符 回车默认逗号");
        String delimiter = new Scanner(System.in).nextLine();


        SysDbConnInfo connInfo = connInfos.get(check2);
        importInfo.setConnName(connInfo.getConnName());
        importInfo.setTableName(tableName);
        importInfo.setFields(fieldStr.split(","));
        importInfo.setFilePath(fileName);
        importInfo.setDelimiter(delimiter);
        try {
            service.imp(importInfo);
            System.out.println("执行结束,执行成功");
        }catch (Exception e){
            System.out.println("执行结束，执行失败,失败原因:");
            System.out.println(e.getMessage());
        }

    }


    public static void excelImp(){
        System.out.println("请选中需要导入的数据库");
        List<SysDbConnInfo> connInfos = DeanToolDao.getInstance().queryConnInfo();
        for (int i = 0; i < connInfos.size(); i++) {
            System.out.println(i+"."+connInfos.get(i).getConnName());
        }
        // conn 连接选择
        String userInput = new Scanner(System.in).nextLine();
        if(!StringUtils.isNumeric(userInput)){
            System.out.println("非法操作,将返回上一步操作");
            excelImp();
            return;
        }
        DataFileImpExpService service = new ExcelImpExpServiceImpl();
        DeanImportInfo importInfo = new DeanImportInfo();
        // 导入文件名
        int check2 = Integer.parseInt(userInput);
        // 导入表名
        System.out.println("请输入要导入的表名");
        String tableName = new Scanner(System.in).nextLine();
        // 导入字段
        System.out.println("请输入要导入的字段名,字段名用逗号分割");
        String fieldStr = new Scanner(System.in).nextLine();
        // 导入文件名
        System.out.println("请输入要导入的文件名");
        String fileName = new Scanner(System.in).nextLine();

        SysDbConnInfo connInfo = connInfos.get(check2);
        importInfo.setConnName(connInfo.getConnName());
        importInfo.setTableName(tableName);
        importInfo.setFields(fieldStr.split(","));
        importInfo.setFilePath(fileName);
        try {
            service.imp(importInfo);
            System.out.println("执行结束,执行成功");
        }catch (Exception e){
            System.out.println("执行结束，执行失败,失败原因:");
            System.out.println(e.getMessage());
        }

    }

    public static void excelExp(){
        System.out.println("请选择导出数据库连接");
        List<SysDbConnInfo> connInfos = DeanToolDao.getInstance().queryConnInfo();
        for (int i = 0; i < connInfos.size(); i++) {
            System.out.println(i+"."+connInfos.get(i).getConnName());
        }
        // conn 连接选择
        String userInput = new Scanner(System.in).nextLine();
        if(!StringUtils.isNumeric(userInput)){
            System.out.println("非法操作,将返回上一步操作");
            excelExp();
            return;
        }
        int check2 = Integer.parseInt(userInput);
        SysDbConnInfo connInfo = connInfos.get(check2);
        System.out.println("请输入导出文件路径");
        String filePath = new Scanner(System.in).nextLine();
        System.out.println("请输入执行的sql脚本");
        String sql = new Scanner(System.in).nextLine();
        try{
            DataFileImpExpService service = new ExcelImpExpServiceImpl();
            DeanExportInfo info = new DeanExportInfo();
            info.setConnName(connInfo.getConnName());
            info.setFilePath(filePath);
            info.setQuerySql(sql);
            service.exp(info);
            System.out.println("执行结束,执行成功");
        }catch (Exception e){
            System.out.println("执行结束，执行失败,失败原因:");
            System.out.println(e.getMessage());
        }
    }

    public static void csvExp(){
        System.out.println("请选择导出数据库连接");
        List<SysDbConnInfo> connInfos = DeanToolDao.getInstance().queryConnInfo();
        for (int i = 0; i < connInfos.size(); i++) {
            System.out.println(i+"."+connInfos.get(i).getConnName());
        }
        // conn 连接选择
        String userInput = new Scanner(System.in).nextLine();
        if(!StringUtils.isNumeric(userInput)){
            System.out.println("非法操作,将返回上一步操作");
            csvExp();
            return;
        }
        int check2 = Integer.parseInt(userInput);
        SysDbConnInfo connInfo = connInfos.get(check2);
        System.out.println("请输入导出文件路径");
        String filePath = new Scanner(System.in).nextLine();
        System.out.println("请输入执行的sql脚本");
        String sql = new Scanner(System.in).nextLine();
        try{
            DataFileImpExpService service = new CsvImpExpServiceImpl();
            DeanExportInfo info = new DeanExportInfo();
            info.setConnName(connInfo.getConnName());
            info.setFilePath(filePath);
            info.setQuerySql(sql);
            info.setDelimiter(",");
            service.exp(info);
            System.out.println("执行结束,执行成功");
        }catch (Exception e){
            System.out.println("执行结束，执行失败,失败原因:");
            System.out.println(e.getMessage());
        }
    }
}
